import 'package:flutter/material.dart';
import 'package:schoollifedemo/business/personal/viewmodel/settings_page_viewmodel.dart';
import 'package:schoollifedemo/commonwidget/circle_empty_widget.dart';
import 'package:schoollifedemo/commonwidget/common_base_page.dart';
import 'package:schoollifedemo/commonwidget/common_dialog.dart';
import 'package:schoollifedemo/commonwidget/text_widget_with_overflow.dart';

class SettingsPage extends BaseStatefulPage {
  const SettingsPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _SettingsPageState();
  }
}

class _SettingsPageState extends BasePageState {
  final SettingsPageViewModel _viewModel = SettingsPageViewModel();
  bool _messagePushEnable = false; // 消息推送
  bool _siteMessagePushEnable = false; // 站内消息

  final Color _defaultColor = const Color(0x4D88B5FA);
  final Color _selectColor = const Color(0xFF80B2FD);
  final Color _transparentColor = Colors.transparent;

  final Color _exitColor = const Color(0x50FA7878);
  final Color _defaultInsideColor = const Color(0x66D0E7FF);

  @override
  void initState() {
    _viewModel.init();
    super.initState();
  }

  @override
  void dispose() {
    _viewModel.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: ListView(
          physics: const BouncingScrollPhysics(),
          children: [
            const Padding(
              padding: EdgeInsets.only(top: 10),
              child: TextWidgetWithOverflow(
                '通用设置',
                fontSize: 28,
                paddingLeft: 25,
                insideColor: Colors.transparent,
                textAlign: TextAlign.left,
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(left: 20, right: 20),
              child: CircleEmptyWidget(
                _commonSettings(),
                insideColor: _defaultInsideColor,
              ),
            ),
            const Padding(
              padding: EdgeInsets.only(top: 10),
              child: TextWidgetWithOverflow(
                '消息设置',
                fontSize: 28,
                paddingLeft: 25,
                insideColor: Colors.transparent,
                textAlign: TextAlign.left,
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(
                  top: 10, bottom: 5, right: 20, left: 20),
              child: GestureDetector(
                child: CircleEmptyWidget(
                  _messageSwitch(
                    title: '消息推送',
                    isEnable: _messagePushEnable,
                    disableText: '关',
                    enableText: '开',
                  ),
                  insideColor: Colors.transparent,
                ),
                onTap: () {
                  _messagePushEnable = !_messagePushEnable;
                  // 设置消息推送的内容
                  setState(() {});
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(
                  top: 5, bottom: 10, left: 20, right: 20),
              child: GestureDetector(
                child: CircleEmptyWidget(
                  _messageSwitch(
                    title: '站内消息',
                    isEnable: _siteMessagePushEnable,
                    disableText: '关',
                    enableText: '开',
                  ),
                  insideColor: Colors.transparent,
                ),
                onTap: () {
                  _siteMessagePushEnable = !_siteMessagePushEnable;
                  // 设置消息推送的内容

                  setState(() {});
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _commonSettings() {
    return Column(
      children: [
        Padding(
          padding:
              const EdgeInsets.only(left: 20, right: 20, top: 10, bottom: 5),
          child: GestureDetector(
            child: const TextWidgetWithOverflow(
              '软件问题提交',
              fontSize: 16,
              height: 40,
              width: double.infinity,
            ),
            onTap: () {
              // 跳转到软件问题提交页面
              _viewModel.turnToSoftwareProblemSubmitPage(context);
            },
          ),
        ),
        Padding(
          padding:
              const EdgeInsets.only(left: 20, right: 20, top: 5, bottom: 10),
          child: GestureDetector(
            child: TextWidgetWithOverflow(
              '退出登录',
              fontSize: 16,
              height: 40,
              insideColor: _exitColor,
              width: double.infinity,
            ),
            onTap: () async {
              bool? trueOrFalse = await DialogUtils.showCancelAndConfirmDialog(
                context,
                title: '确认',
                msg: '是否退出登录',
                clickOutSideDismiss: true,
              );
              if (trueOrFalse == true) {
                _viewModel.logout(context);
              }
            },
          ),
        ),
      ],
    );
  }

  Widget _messageSwitch({
    required String title,
    required bool isEnable,
    required String disableText,
    required String enableText,
  }) {
    return CircleEmptyWidget(
      Container(
        margin: const EdgeInsets.only(top: 10, bottom: 10, left: 5, right: 5),
        child: Row(
          children: [
            TextWidgetWithOverflow(
              title,
              fontSize: 16,
              insideColor: Colors.transparent,
              width: 120,
            ),
            Expanded(
              child: CircleEmptyWidget(
                Row(
                  children: [
                    Expanded(
                      child: TextWidgetWithOverflow(
                        disableText,
                        fontSize: 16,
                        insideColor:
                            !isEnable ? _selectColor : _transparentColor,
                      ),
                    ),
                    Expanded(
                      child: TextWidgetWithOverflow(
                        enableText,
                        fontSize: 16,
                        insideColor:
                            isEnable ? _selectColor : _transparentColor,
                      ),
                    ),
                  ],
                ),
                insideColor: _defaultColor,
              ),
            ),
          ],
        ),
      ),
      insideColor: _defaultInsideColor,
    );
  }
}
